<?php
/**
 * @ClassName: Appversion
 * @Description: 版本更新
 * @author: wyaohu
 * @Date: 2024/09/11 19:19
 * @Version: V1.0.0
 */

namespace app\admin\controller;

use app\common\model;
use think\Controller;
use tpext\builder\traits\HasBuilder;

class Appversion extends Controller
{
    use HasBuilder;

    protected $dataModel;

    protected function initialize()
    {
        $this->dataModel = new model\AppVersion();
        $this->pageTitle = '版本更新';
        $this->sortOrder = 'new_version desc';

        $this->selectSearch = 'new_version';
    }

    /**
     * 构建搜索
     *
     * @return void
     */
    protected function buildSearch()
    {
        $search = $this->search;

        $search->text('new_version', '版本', 3)->maxlength(20);
    }

    /**
     * 构建搜索条件
     * @return array
     */
    protected function filterWhere()
    {
        $searchData = request()->get();

        $where = [];

        if (!empty($searchData['new_version'])) {
            $where[] = ['new_version', 'like', '%' . $searchData['new_version'] . '%'];
        }

        return $where;
    }

    /**
     * 构建表格
     *
     * @return void
     */
    protected function buildTable(&$data = [])
    {
        $table = $this->table;

        $table->show('id', 'ID');
        $table->show('new_version', '版本')->autoPost();
        $table->match('app_type', 'APP类型')->options(model\AppVersion::$app_types);
        $table->match('platform_type', '平台类型')->options(model\AppVersion::$platform_types);
        $table->show('update_text', '更新内容');
        $table->show('download_url', '下载地址');
        $table->fields('times_group', '添加/修改时间')->with(
            $table->show('create_time', '添加时间')->default('---'),
            $table->show('update_time', '修改时间')->default('---')
        )->getWrapper()->addStyle('width:160px');

        $table->sortable('id, create_time');

        $table->getToolbar()
            ->btnAdd()
            ->btnRefresh();

        $table->getActionbar()
            ->btnEdit()
//            ->btnDelete()
            ->btnView();
    }

    /**
     * 构建表单
     *
     * @param boolean $isEdit
     * @param array $data
     */
    protected function buildForm($isEdit, &$data = [])
    {
        $form = $this->form;

        $form->hidden('id');
        $form->text('new_version', '版本')->maxlength(20);
        $form->radio('app_type', 'APP类型')->options(model\AppVersion::$app_types)->default('user');
        $form->radio('platform_type', '平台类型')->options(model\AppVersion::$platform_types)->default('android');
        $form->textarea('update_text', '更新内容')->maxlength(500);
        $form->text('download_url', '下载地址')->maxlength(255)->default(domain() . '/download/app/user.apk')
            ->help('APP下载地址：' . domain() . '/download/app/user.apk');

        if ($isEdit) {
            $form->show('create_time', '添加时间');
            $form->show('update_time', '更新时间');
        }
    }

    /**
     * 保存数据
     *
     * @param integer $id
     * @return void
     */
    private function save($id = 0)
    {
        $data = request()->only([
            'new_version',
            'app_type',
            'platform_type',
            'update_text',
            'download_url',
        ], 'post');

        $result = $this->validate($data, [
            'new_version|版本' => 'require',
            'app_type|APP类型' => 'require',
            'platform_type|平台类型' => 'require',
            'update_text|更新内容' => 'require',
            'download_url|下载地址' => 'require',
        ]);

        if (!$result) {
            $this->error($result);
        }

        $data['version_code'] = intval(str_replace(".", "", $data['new_version']));

        if ($id) {
            // 编辑
            $data['update_time'] = date_now();
        } else {
            // 新增
            $data['create_time'] = date_now();
            $data['update_time'] = date_now();
        }

        return $this->doSave($data, $id);
    }

}